home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Tool Chest / Testing & Debugging / Mac OS Development Toolkit / Automation Essentials 2.3.0 / Host Automation Folder / SPEC Libs / String.Lib < prev    next >
Encoding:
Text File  |  1998-03-19  |  14.9 KB  |  421 lines  |  [TEXT/MPS ]

  1. #
  2. # ****************************************************************************
  3. #
  4. #    File Name:        String.Lib
  5. #
  6. #    Contains:    xxx put contents here xxx
  7. #
  8. #    Written by:    KTA, KL, ML, GS et al
  9. #
  10. #    Copyright:    © 1993-1995 by Apple Computer, Inc., all rights reserved.
  11. #
  12. # ****************************************************************************
  13. #            C h a n g e        H i s t o r y (most recent first):
  14. # ****************************************************************************
  15. #
  16. #        Vers      Date        Author        Description
  17. #        ----    --------    ------    ---------------------------------------------
  18. #     <1.0.6>     5/16/95    ML        Added task ConcatNameString()
  19. #     <1.0.5>     2/28/95    KTA        Fixed marks, added TruncateString().
  20. #     <1.0.4>     2/10/95    KTA        SubString() - Changed header to indicate that input parameter
  21. #                                    <StartChar> is an integer and not a char.
  22. #     <1.0.3>    12/21/94    KTA        SubString() - Fixed parenthesis in wrong place bug.
  23. #     <1.0.2>     8/20/93    KTA        Added StringUntilChar(), StripCarriageReturn(),
  24. #                                    NumTimesCharInString(), StringAfterChar() - for better filepath
  25. #                                    parsing.
  26. #        <1+>     5/21/93    NAGA        Adding header and porting old files to follow new standards
  27. #
  28. # ****************************************************************************
  29. #
  30.  
  31. #########################################################################
  32. #                             IsSubString(str1, str2)
  33. #=======================================================================
  34. # Author:              SL
  35. # Description:        Checks to see if str1 is in str2.
  36. # Parameters:        str1    :=    Substring to be searched for.
  37. #                    str2    :=    String.
  38. # Returns:            0        :=    str1 is not in str2.
  39. #                    1        :=    str1 is in str2.
  40. #=======================================================================
  41. # History:
  42. #
  43. #########################################################################
  44. TASK IsSubString(str1, str2) begin
  45.      if (str2 = str1) or ( str2 ~= /≈"{str1}"≈/) return(1);
  46.      else return(0);
  47. end; # isSubString()
  48.  
  49. #########################################################################
  50. #                             PointListToStr(pts)
  51. #=======================================================================
  52. # Author:              DM
  53. # Description:        Converts a list of points to a string.
  54. # Parameters:        pt    :=    List of coordinate pairs.
  55. # Returns:            0        :=    if the conversion fails.
  56. #                    1        :=    The pt as a string.
  57. #=======================================================================
  58. # History:
  59. #
  60. #########################################################################
  61. TASK PointListToStr(pointList := {}) begin
  62.     returnVal := O;
  63.     if(pointList) begin
  64.         pointStr := "";
  65.         pointCount := card(pointList);
  66.         for index := 1 to (pointCount-1) begin
  67.             point := pointList[index];
  68.             x := point[1];
  69.             y := point[2];
  70.             pointStr := "{pointStr}∂{{x},{y}∂},";
  71.         end;
  72.         point := pointList[index];
  73.         x := point[1];
  74.         y := point[2];
  75.         pointStr := "∂{{pointStr}∂{{x},{y}∂}∂}";
  76.         returnVal := pointStr;
  77.     end;
  78.     else
  79.         Println "The list passed to PointListToStr is empty";
  80.     return(returnVal);
  81. end; # PtToStr()
  82.  
  83. #########################################################################
  84. #                         FindPos(char, str, startPos)
  85. #=======================================================================
  86. # Author:          SL
  87. # Description:    Finds the first occurence of char in str starting
  88. #                from startPos.
  89. # Parameters:    char    :=    A character to be searched for.
  90. #                str        :=     String.
  91. #                startPos:=    Starting position to be searched.
  92. # Returns:        0        :=    Char is not in str starting from
  93. #                            startPos.
  94. #                pos        :=     Char position of its first occurence
  95. #                            starting from startPos.
  96. #=======================================================================
  97. # History:
  98. #
  99. #########################################################################
  100. TASK FindPos(char, str, startPos:= 1)
  101. begin
  102.     numStr := card str;
  103.     
  104.     # If start position is greater than the string length, return 0 #
  105.     if (startPos > numStr)
  106.         return(0);
  107.     
  108.     for i:= startPos to numStr do
  109.         begin
  110.             if (char = str[i])
  111.                 return(i);
  112.         end; # for
  113.     return(0);
  114. end; # findPos()
  115.  
  116. #########################################################################
  117. #                         Substring(String,StartChar,NumChar)
  118. #=======================================================================
  119. # Author:          PF 
  120. # Description:    Returns the first <NumChar> characters of the passed <String>
  121. #                starting at position <StartChar>.
  122. # Parameters:    String - String to use
  123. #                StartChar - integer - position of character to start making substring.
  124. #                            new string will begin with character at this position.
  125. #                NumChar - Integer number of characters to return
  126. # Returns:        string
  127. # Assumptions:    none
  128. #=======================================================================
  129. # History:
  130. # KTA    12/21/94    Fixed parenthesis in wrong place bug.
  131. # KTA    2/10/95        Changed header to indicate that input parameter <StartChar>
  132. #                    is an integer and not a char.
  133. ##########################################################################
  134. TASK SubString(String,StartChar,NumChar) begin
  135.     # first see if number of characters is greater then length of string
  136.     # and return the whole end of the string if it is.
  137.     if (NumChar > (card(String ) - StartChar + 1)) begin
  138.         return (Substring (String,StartChar,(card(String) - StartChar) +1));
  139.         
  140.     end;
  141.     else begin
  142.         # create a string of first NumChar characters and return it.
  143.         NewString:="";                            #start with an empty string
  144.         for CharCount :=StartChar to ((StartChar + NumChar) - 1)
  145.             NewString := NewString + String[CharCount];
  146.             return (NewString);
  147.     end;
  148. end; #Substring Task
  149.  
  150. #########################################################################
  151. #                        RandomString( NumChar )
  152. #========================================================================
  153. # Author:        KTA 
  154. # Description:    Generate Random strings <NumChar> long.
  155. # Parameters:    NumChar := Length of random string to generate
  156. # Returns:        The random string.
  157. # Examples:        RandomString( 5 ); - Random String 5 characters in length
  158. # Assumptions:    That you want Caps, lower case, numbers and punctuation
  159. #========================================================================
  160. # History:
  161. #
  162. #########################################################################
  163. TASK RandomString(NumChar := 3) 
  164. begin
  165.     RandStr := '';
  166.         #FullcharSTRING := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&()_-=+{[}]"';></?`\'~;        # All Caps    
  167.         ALLCAPSString := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';        # ALL CAPS    
  168.         lowercaseString := 'abcdefghijklmnopqrstuvwxyz';    # lower case
  169.         NumberString := '0123456789';                        # Numbers
  170.         PunctuationString := " !@#$%^&()_-∂{∂}'~`";            # Punctuaton
  171.         
  172.         charSTRING := ALLCAPSString + lowercaseString + NumberString + PunctuationString;
  173.     for NumTimes := 1 to NumChar begin                    # How many characters
  174.         WhichItem := Random(1,Card charSTRING);            # Get a random index into the charString
  175.         NewChar := charSTRING[WhichItem];                # Get a character
  176.         RandStr := RandStr + NewChar;                    # Concat to the String
  177.     end;
  178.     Return(RandStr);
  179. end; # RandomString()
  180.  
  181. #############################################################################
  182. TASK FormPositiveInteger(str, start_index := 1, end_index := -1)
  183. begin
  184.     num := 0;
  185.     if (end_index = -1) end_index := card(str);
  186.     for i:= start_index to end_index
  187.     begin
  188.         digit := CharToDigit(str[i]); 
  189.         if (digit = -1) return -1; #error condition
  190.         else if (num > 3276) return -1; #overflow condition
  191.         else if (num = 3276) and (digit > 7) return -1; #overflow condition
  192.         num := num*10 + digit;
  193.     end;#for each digit
  194.     return num;
  195. end;
  196. #############################################################################
  197. TASK CharToDigit(character)
  198. begin
  199.     if (character = '0')
  200.         return (0);
  201.     else if (character = '1')
  202.         return(1);
  203.     else if (character = '2')
  204.         return(2);
  205.     else if (character = '3')
  206.         return(3);
  207.     else if (character = '4')
  208.         return(4);
  209.     else if (character = '5')
  210.         return(5);
  211.     else if (character = '6')
  212.         return(6);
  213.     else if (character = '7')
  214.         return(7);
  215.     else if (character = '8')
  216.         return(8);
  217.     else if (character = '9')
  218.         return(9);
  219.     else return (-1); #error condition
  220. end;
  221.  
  222. #########################################################################
  223. #                    StripCarriageReturn(pLine)
  224. #========================================================================
  225. # Author:        KTA 
  226. # Description:    returns a string that contains all of the characters in 
  227. #                <pLine> up to but not including the carriage return.
  228. # Parameters:    pLine - The line of text
  229. # Returns:        string without the carriage return
  230. # Examples:        StripCarriageReturn();
  231. # Assumptions:    None 
  232. #========================================================================
  233. # History:
  234. #
  235. #########################################################################
  236. TASK StripCarriageReturn(pLine)
  237. begin
  238.     return(StringUntilChar(pLine, "∂n",0));
  239. end;
  240.  
  241. #########################################################################
  242. #        StringUntilChar(pTheString, pTheChar, pIncludeTheChar, pNumOccurences)
  243. #========================================================================
  244. # Author:        KTA 
  245. # Description:    returns a string that contains all of the characters in 
  246. #                <pTheString> up to <pTheChar>. If <pIncludeTheChar> evaluates to
  247. #                true <pTheChar> will be included in the returned string.
  248. # Parameters:    pTheString - The string  
  249. #                pTheChar - the character to search for
  250. #                pIncludeTheChar - Boolean indicates whether or not to include 
  251. #                                <pTheChar>.
  252. #                pNumOccurences - indicates how many times <pChar> should occur
  253. #                                prior to returning the string.
  254. # Returns:        returns a string that contains all of the characters in 
  255. #                <pTheString> up to (but not necessarily including) <pTheChar>. 
  256. # Examples:        StringUntilChar();
  257. # Assumptions:    None 
  258. #========================================================================
  259. # History:
  260. #
  261. #########################################################################
  262. TASK StringUntilChar(pTheString, pTheChar, pIncludeTheChar := 0, pNumOccurences := 1)
  263. begin
  264.     newString := '';
  265.     actualOccurrence := 0;
  266.     if(pTheString)
  267.     begin
  268.         for i := 1 to Card(pTheString)
  269.         begin
  270.             if not (pTheString[i] = pTheChar)
  271.                     newString := newString + pTheString[i];
  272.             else
  273.             begin
  274.                 actualOccurrence := actualOccurrence +1;
  275.                 if (actualOccurrence = pNumOccurences)
  276.                 begin
  277.                     if(pIncludeTheChar)
  278.                         newString := newString + pTheString[i];
  279.         
  280.                     return (newString);
  281.                 end;
  282.                 else
  283.                     newString := newString + pTheString[i];
  284.             end;
  285.         end;
  286.     end;
  287.     return (newString);
  288. end;
  289.  
  290. #########################################################################
  291. #                NumTimesCharInString(pTheString, pTheChar)
  292. #========================================================================
  293. # Author:        KTA 
  294. # Description:    returns an integer that indicates the number of times <pChar>
  295. #                occurs in <pTheString>.
  296. # Parameters:    pTheString - The string  
  297. #                pTheChar - the character to search for
  298. # Returns:        returns an integer that indicates the number of times <pChar>
  299. #                occurs in <pTheString>. 
  300. # Examples:        NumTimesCharInString("hd:thisFolder:ThatFolder:FileName", ":");
  301. # Assumptions:    None 
  302. #========================================================================
  303. # History:
  304. #
  305. #########################################################################
  306. TASK NumTimesCharInString(pTheString, pTheChar)
  307. begin
  308.     actualOccurrence := 0;
  309.     for i := 1 to Card(pTheString)
  310.     begin
  311.         if (pTheString[i] = pTheChar)
  312.             actualOccurrence := actualOccurrence +1;
  313.     end;
  314.     return (actualOccurrence);
  315. end;
  316.  
  317. #########################################################################
  318. #        StringAfterChar(pTheString, pTheChar, pIncludeTheChar, pNumOccurences)
  319. #========================================================================
  320. # Author:        KTA 
  321. # Description:    returns a string that contains all of the characters in 
  322. #                <pTheString> after <pTheChar>. If <pIncludeTheChar> evaluates to
  323. #                true <pTheChar> will be included in the returned string.
  324. # Parameters:    pTheString - The string  
  325. #                pTheChar - the character to search for
  326. #                pIncludeTheChar - Boolean indicates whether or not to include 
  327. #                                <pTheChar>.
  328. #                pNumOccurences - indicates how many times <pChar> should occur
  329. #                                prior to creating the returned string. 
  330. # Returns:        returns a string that contains all of the characters in 
  331. #                <pTheString> after <pTheChar>. 
  332. # Examples:        StringAfterChar();
  333. # Assumptions:    None 
  334. #========================================================================
  335. # History:
  336. #
  337. #########################################################################
  338. TASK StringAfterChar(pTheString, pTheChar, pIncludeTheChar := 0, pNumOccurences := 1)
  339. begin
  340.     newString := '';
  341.     actualOccurrence := 0;
  342.     for i := 1 to Card(pTheString)
  343.     begin
  344.         if not(CreateStringFlag)
  345.         begin
  346.             if (pTheString[i] = pTheChar)
  347.             begin
  348.                 actualOccurrence := actualOccurrence +1;
  349.                 if(pNumOccurences = actualOccurrence)
  350.                 begin
  351.                     if(pIncludeTheChar)
  352.                         newString := newString + pTheString[i];
  353.                     CreateStringFlag := 1;
  354.                 end;
  355.             end;
  356.         end;
  357.         else
  358.             newString := newString + pTheString[i];
  359.     end;
  360.     return (newString);
  361. end;
  362.  
  363. #########################################################################
  364. #                TruncateString(pTheString, pNumChars)
  365. #========================================================================
  366. # Author:        KTA 
  367. # Description:    Truncates the string <pTheString> to be no more than <pNumChars>
  368. #                in length.
  369. # Parameters:    pTheString - The string  
  370. #                pNumChars - the number of character desired.
  371. # Returns:        returns a string which contains no more than <pNumChars> characters. 
  372. # Examples:        TruncateString("HelloWorld", 4);
  373. # Assumptions:    None 
  374. #========================================================================
  375. # History:
  376. #
  377. #########################################################################
  378. TASK TruncateString(pTheString, pNumChars)
  379. begin
  380.     returnVal := pTheString;
  381.     numChars := Card(pTheString);
  382.     if (numChars > pNumChars)
  383.     begin
  384.         tempStr := '';
  385.         for i := 1 to pNumChars
  386.             tempStr := tempStr + pTheString[i];
  387.         
  388.         returnVal := tempStr;
  389.     end;
  390.     return(returnVal);
  391. end;
  392.  
  393. #########################################################################
  394. #                ConcatNameString(pMainString, pAppendString, pMaxChars)
  395. #========================================================================
  396. # Author:        ML
  397. # Description:    Concats a main string and append string, checking to see if
  398. #                if falls withing allowable limits and truncating main string
  399. #                if it doesn't.
  400. # Parameters:    pMainString - The main string.  Will be truncated if
  401. #                concatenated string is too long
  402. #                pAppendString - the string to append.  Will not be truncated.
  403. #                pMaxChars - the maximum number of characters allowed.
  404. # Returns:        concatenated string.
  405. # Examples:        TruncateString("Target Crash File", "1215", 31);
  406. # Assumptions:    None 
  407. #========================================================================
  408. # History:
  409. # 5/16/95    ML    created
  410. #########################################################################
  411. TASK ConcatNameString(pMainString, pAppendString, pMaxChars)
  412. begin
  413.     numCharsMainString := Card(pMainString);
  414.     numCharsAppendString := Card(pAppendString);
  415.     if ((numCharsMainString + numCharsAppendString) > pMaxChars)
  416.         pMainString := TruncateString(pMainString,
  417.                                         (pMaxChars-numCharsAppendString));
  418.     return(pMainString + pAppendString);
  419. end;
  420.  
  421.